package com.yangjian.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yangjian.bean.Setmeal;
import com.yangjian.comon.R;
import com.yangjian.dto.SetmealDto;
import com.yangjian.page.PageParameter;
import com.yangjian.service.SetmealService;
import jdk.nashorn.internal.runtime.Debug;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class SetmealController {


    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping("/setmeal")
    public R add(@RequestBody SetmealDto setmealDto){

        int row = setmealService.add(setmealDto);
        if (row>0){
            return R.success("添加套餐成功!");
        }else {
            return R.error("添加套餐失败");
        }
    }


    /**
     * 分页查询套餐
     * @param pageParameter
     * @return
     */
    @GetMapping("/setmeal/page")
    public R findPage(PageParameter pageParameter){
        try {
            IPage<SetmealDto> page = setmealService.findPage(pageParameter);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }
    }

    /**
     * 根据id删除套餐
     * @param ids
     * @return
     */

    @DeleteMapping("/setmeal")
    public R delete(@RequestParam List<Long> ids){
        int row = setmealService.delete(ids);
        if (row>0){
            return R.success("删除套餐成功!");

        }else {
            return R.error("删除套餐失败！！！");
        }

    }

    /**
     * 修改套餐的售卖状态
     * @param status
     * @param ids
     * @return
     */

    @PostMapping("/setmeal/status/{status}")
    public R updateStatus(@PathVariable("status") int status,@RequestParam List<Long> ids){

        System.out.println("status = " + status);
        System.out.println("ids = " + ids);

        int row = setmealService.updateStatus(status, ids);

        if (row > 0) {

            return R.success("修改套餐售卖状态成功");
        } else {

            return R.error("修改套餐售卖状态失败");
        }
    }

    /**
     * 根据分类id和状态来查询套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/setmeal/list")
    public R findSetmeal(Long categoryId,Integer status){
        log.debug("*****根据分类id和状态来查询套餐******");
        List<Setmeal> setmeal = setmealService.findSetmeal(categoryId, status);
        return R.success(setmeal);

    }

}
